package com.xhh.nphm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhh.nphm.system.domain.KbmCateM;
import com.xhh.nphm.system.domain.KbmPointM;
import com.xhh.nphm.system.domain.NodeCate;
import com.xhh.nphm.system.mapper.KbmCateMDao;
import com.xhh.nphm.system.mapper.KbmPointMDao;
import com.xhh.nphm.system.service.IKbmManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class IKbmManageServiceImpl implements IKbmManageService {

    @Autowired
    private KbmCateMDao kbmCateMDao;

    @Autowired
    private KbmPointMDao kbmPointMDao;

    @Override
    public List<KbmCateM> kbmCateListJson(KbmCateM kbmCate) {
        return kbmCateMDao.kbmCateListJson(kbmCate);
    }

    @Override
    public List<Map<String, Object>> kbmPointListJson(KbmPointM kbmPoint) {
        return kbmPointMDao.kbmPointListJson(kbmPoint);
    }

    @Override
    public KbmCateM findCateById(String codePk) {
        return kbmCateMDao.findCateById(codePk);
    }

    @Override
    public KbmPointM findPointById(String pkId) {
        return kbmPointMDao.findPointById(pkId);
    }

    @Override
    public int updateKbmPointStatus(KbmPointM kbmPoint) {
        return kbmPointMDao.updateById(kbmPoint);
    }

    @Override
    public int saveKbmPoint(KbmPointM kbmPoint) {
        return kbmPointMDao.insert(kbmPoint);
    }

    @Override
    public int updateKbmPoint(KbmPointM kbmPoint) {
        return kbmPointMDao.updateById(kbmPoint);
    }

    @Override
    public int kbmCateEditSave(KbmCateM kbmCate) {
        return kbmCateMDao.updateByCodePK(kbmCate);
    }

    @Override
    public int kbmPointRemove(String pkId) {
        return kbmPointMDao.kbmPointRemove(pkId);
    }

    @Override
    public int selectPointByCodePK(String codePk) {
        return kbmPointMDao.selectPointCount(codePk);
    }

    @Override
    public int selectSonCateByCodePk(String codePk) {
        return kbmCateMDao.selectCateByCodePk(codePk);
    }

    @Override
    public int removeCateByCodePk(String codePk) {
        return kbmCateMDao.removeCateByCodePk(codePk);
    }

    @Override
    public KbmCateM selectCateById(String codePk) {
        return kbmCateMDao.selectByCodePk(codePk);
    }

    @Override
    public String checkCodePkUnique(KbmCateM kbmCate) {
        return kbmCateMDao.checkCodePkUnique(kbmCate) > 0 ? "1" : "0";
    }

    @Override
    public String checkSortNameUnique(KbmCateM kbmCate) {
        return kbmCateMDao.checkSortNameUnique(kbmCate) > 0 ? "1" : "0";
    }

    @Override
    public int kbmCateAddSave(KbmCateM kbmCate) {
        return kbmCateMDao.insert(kbmCate);
    }

    @Override
    public List<NodeCate> findCateList() {
        return getChildPerms(kbmCateMDao.kbmCateList(), "-1");
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<NodeCate> getChildPerms(List<NodeCate> list, String parentId) {
        List<NodeCate> returnList = new ArrayList<NodeCate>();
        for (Iterator<NodeCate> iterator = list.iterator(); iterator.hasNext(); ) {
            NodeCate t = (NodeCate) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getPid().equals(parentId)) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<NodeCate> list, NodeCate t) {
        // 得到子节点列表
        List<NodeCate> childList = getChildList(list, t);
        t.setChildren(childList);
        for (NodeCate tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<NodeCate> getChildList(List<NodeCate> list, NodeCate t) {
        List<NodeCate> tlist = new ArrayList<NodeCate>();
        Iterator<NodeCate> it = list.iterator();
        while (it.hasNext()) {
            NodeCate n = (NodeCate) it.next();
            if (n.getPid().equals(t.getId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<NodeCate> list, NodeCate t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
